package com.api.oa.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.api.oa.exception.BadRequestException;
import com.api.oa.model.entity.FileList;
import com.api.oa.model.entity.YQInsEntity;
import com.api.oa.services.YQInsEntityService;
import com.api.shopapi.controller.BaseController;
import com.api.shopapi.entity.MyResult;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/yq/yQInsEntity")
public class YQInsEntityController extends BaseController {

    @Autowired
    private YQInsEntityService iYQInsEntityService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody YQInsEntity entity) {

        try {
            entity.setCreatedAt(DateUtil.date());
            return iYQInsEntityService.add(entity) == 1 ? success(entity) : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/addBatch", method = RequestMethod.POST)
    public MyResult Add(@Validated @RequestBody List<YQInsEntity> entities, HttpServletRequest req) {
        try {
            String timestamp = req.getHeader("timestamp");
            final String sign = req.getHeader("sign");
            if (StrUtil.isEmpty(timestamp)) return failed("时间戳不能为空");
            if (StrUtil.isEmpty(sign)) return failed("认证未通过");
            String format = String.format("timestamp=%s&key=%s", timestamp, privateKey);
            String s = SecureUtil.md5(format);
            if (!s.equals(sign)) return failed("认证未通过");
            return iYQInsEntityService.addBatch(entities) == 1 ? success("添加成功") : failed("操作失败");
        } catch (BadRequestException e) {
            return anomalous(e.getMessage());
        }

    }


    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    public MyResult remove(@PathVariable String id) {
        try {
            iYQInsEntityService.remove(id);
            return success("删除成功!");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public MyResult update(@RequestBody YQInsEntity entity) {
        try {
            return iYQInsEntityService.update(entity) == 1 ? success("操作成功") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MyResult list(@RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize,
                         @RequestParam(defaultValue = "") String insCode, @RequestParam(required = false) String orgId) {
        try {
            return success(iYQInsEntityService.page(insCode, orgId, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(@RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize,
                       @RequestParam(defaultValue = "") String insCode, @RequestParam(required = false) String orgId, HttpServletResponse res) throws IOException {
        iYQInsEntityService.export(insCode, orgId, pageIndex, pageSize, res);
    }


    @RequestMapping(value = "/android/list", method = RequestMethod.GET)
    public MyResult listForAndroid(@RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize,
                                   @RequestParam(defaultValue = "") String insCode, @RequestParam(required = false) String orgId) {
        try {
            return success(iYQInsEntityService.androidPage(insCode, orgId, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public MyResult list(@RequestParam(required = false) String insName) {
        try {
            return success(iYQInsEntityService.search(insName));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/one/{id}", method = RequestMethod.GET)
    public MyResult one(@PathVariable String id) {
        try {
            return success(id == null ? null : iYQInsEntityService.one(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/ins/{id}", method = RequestMethod.GET)
    public MyResult listIns(@PathVariable String id) {
        try {
            return success(iYQInsEntityService.listInsByOrgId(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


}
